package com.fishery.memo.job;

import cn.hutool.core.lang.Validator;
import com.alibaba.fastjson.JSON;
import com.fishery.memo.client.MessageClient;
import com.fishery.memo.client.UserClient;
import com.fishery.memo.entity.client.Message;
import com.fishery.memo.entity.client.User;
import com.fishery.memo.service.TaskService;
import com.fishery.memo.util.DateFormat;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author run
 * @since 2021/1/30 10:24
 */
public class SendMessageJob implements Job {

    @Autowired
    private MessageClient messageClient;

    @Autowired
    private UserClient userClient;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        String userId = (String) jobDataMap.get("userId");
        String action = "[ " + jobDataMap.get("content") + " ]";
        String time = DateFormat.format((Time) jobDataMap.get("time"));

        //查找用户的手机号码
        User user = JSON.parseObject(JSON.toJSONString(userClient.searchUserById(userId).getData()), User.class);
        if (user == null || user.getPhone() == null || !Validator.isMobile(user.getPhone())){
            return;
        }

        messageClient.sendRemind(new Message(user.getPhone(),time,action));
    }
}
